﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BCCommon;
using System.Text;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using BCDto.Trading.Enquiry.SaleEnquiry;
using BCTMSRESTful.Configs;
using BCService.Utils;
using BCService.Trading.Enquiry.SaleEnquiry;

namespace BCTMSRESTful.Controllers.Management.Enquiry
{
    /// <summary>
    /// 销售询盘
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class SaleEnquiryController : ManagementBaseController
    {
        private readonly ISaleEnquiryService saleEnquiryService;
        
        public SaleEnquiryController(ISaleEnquiryService saleEnquiryService)
        {
            this.saleEnquiryService = saleEnquiryService;
        }

        #region 添加销售询盘
        /// <summary>
        ///  添加销售询盘
        /// </summary>
        /// <param name="requestDto">销售询盘信息</param>
        /// <response code="201">添加销售询盘成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加销售询盘")]
        [ProducesResponseType(typeof(SaleEnquiryDto), 201)]
        public IActionResult Post(
            [FromBody]SaleEnquiryStaffRequestDto requestDto)
        {
            var result = this.saleEnquiryService.Add(requestDto,this.Admin);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 修改销售询盘
        /// <summary>
        ///  修改销售询盘
        /// </summary>
        /// <param name="saleEnquiryId">销售询盘Id</param>
        /// <param name="requestDto">销售询盘信息</param>
        /// <response code="200">修改销售询盘成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("{sale_enquiry_id}")]
        [Log("修改销售询盘")]
        [ProducesResponseType(typeof(SaleEnquiryDto), 200)]
        public IActionResult Put([FromRoute(Name = "sale_enquiry_id")]long saleEnquiryId, [FromBody]SaleEnquiryStaffRequestDto requestDto)
        {
            SaleEnquiryPutDto putDto = requestDto.As<SaleEnquiryPutDto>();
            putDto.SaleEnquiryId = saleEnquiryId;
            return StatusCode((int)HttpStatusCode.OK, this.saleEnquiryService.Update(putDto));
        }
        #endregion

        #region 删除销售询盘
        /// <summary>
        ///  删除销售询盘
        /// </summary>
        /// <param name="saleEnquiryId">销售询盘Id</param>
        /// <response code="204">删除销售询盘成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpDelete("{sale_enquiry_id}")]
        [Log("删除销售询盘")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "sale_enquiry_id")]long saleEnquiryId)
        {
            if (!this.Admin.IsSystemAccount)
            {
                throw new ArgumentException("权限不足，我发执行此操作!");
            }
            this.saleEnquiryService.Delete(saleEnquiryId);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion

        #region 获取销售询盘
        /// <summary>
        ///  获取销售询盘
        /// </summary>
        /// <param name="saleEnquiryId">销售询盘Id</param>
        /// <response code="200">获取销售询盘</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("{sale_enquiry_id}")]
        [ProducesResponseType(typeof(SaleEnquiryDto), 200)]
        public IActionResult Get([FromRoute(Name = "sale_enquiry_id")]long saleEnquiryId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleEnquiryService.Get(saleEnquiryId));
        }
        #endregion

        #region 获取销售询盘（分页）
        /// <summary>
        ///  获取销售询盘（分页）
        /// </summary>
        /// <param name="adminId">接待人Id</param>
        /// <param name="userName">用户姓名</param>
        /// <param name="phone">用户联系方式</param>
        /// <param name="companyName">公司名称</param>
        /// <param name="weChatNo">微信号</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">当前页大小</param>
        /// <response code="200">获取销售询盘（分页）</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<SaleEnquiryDto>), 200)]
        public IActionResult Query(
            [FromQuery(Name = "receipt_admin_id")]long? adminId,
            [FromQuery(Name = "user_name")]string userName,
            [FromQuery(Name = "phone")]string phone,
            [FromQuery(Name = "company_name")]string companyName,
            [FromQuery(Name = "wechat_no")]string weChatNo,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleEnquiryService.Query(adminId, userName, phone, companyName, weChatNo, start, end, sortName, ascending, index, size,this.Admin));
        }
        #endregion
    }
}

